// +----------------------------------------------------------------------
// | CmsWing [ 网站内容管理框架 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2115 http://www.cmswing.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: arterli <arterli@qq.com>
// +----------------------------------------------------------------------
module.exports = class extends think.cmswing.center {
  constructor(ctx) {
    super(ctx); // 调用父级的 constructor 方法，并把 ctx 传递进去
    // 其他额外的操作
    this.tactive = 'gongxufabu';
  }
  async __before() {
    await super.__before();
    // 判断是否登陆
    // await this.weblogin();
    if (!this.is_login) {
      // 判断浏览客户端
      if (this.isMobile) {
        // 手机端直接跳转到登录页面
        return this.redirect('/center/public/login');
      } else {
        return this.redirect('/');
      }
    }
  }
  // 我的消息
  async indexAction() {
    if (this.is_login > 0) {
      const status = this.para('status') || null;
      // console.log(status);
      const map = {
        appmobile: this.user.mobile,
        status: ['IN', [3, 4]]
      };
      // 当前位置
      if (!think.isEmpty(status)) {
        this.assign('status', status);
        map.status = status;
      }
      const data = await await this.model('cmswing/gongxufabu').where(map).order('id DESC').countSelect();
      const html = this.pagination(data);
      this.assign('pagination', html);
      const statusArr = [3, 4];
      await Promise.all(statusArr.map(async(status) => {
        const count = await this.model('gongxufabu').where({
          status: status,
          appmobile: this.user.mobile
        }).count('id');
        this.assign('status' + status, count);
      }));
      this.assign('count', data.count);
      this.assign('list', data.data);
      this.meta_title = '我的消息';
      // 判断浏览客户端
      if (this.isMobile) {
        if (this.isAjax('get')) {
          for (const v of data.data) {
            v.create_time = this.moment(v.create_time).format('lll');
          }
          return this.json(data);
        } else {
          this.active = 'user/index';
          return this.display(this.mtpl());
        }
      } else {
        return this.display();
      }
    } else {
      return this.redirect('/');
    }
  }

  /**
     * 查看消息
     * @returns {*}
     */
  async detailAction() {
    const id = this.get('id');
    this.meta_title = '查看消息详情';

    if (this.is_login > 0) {
      // 获取发布信息
      const gongxufabu = await this.model('gongxufabu').find(id);
      this.assign('gongxufabu', gongxufabu);
      this.active = ['/shangping?mokuaibiaoshi=' + gongxufabu.mokuaibiaoshi];
      const url = gongxufabu.tupian ? JSON.parse(gongxufabu.tupian) : [];
      this.assign('url', url);
      // 发布人信息
      const user = await this.model('member').where({id: gongxufabu.faburen}).find();
      this.assign('user', user);
    } else {
      return this.redirect('/');
    }

    return this.display();
  }

  // 挂单确认和取消
  async appjiedanAction() {
    const id = this.post('id');
    const userid = this.post('userid');
    const queren = this.post('queren');
    if (id) {
      const gongxuInfo = await this.model('gongxufabu').where({id: id}).find();
      if (think.isEmpty(gongxuInfo)) {
        think.logger.info('appjiedanAction:供需信息为空:', gongxuInfo);
        return this.fail('确认失败');
      }
      if (queren == 0) {
        const res = await this.model('gongxufabu').where({id: id}).update({status: 4});
        if (!res) { return this.fail('确认失败') }
        if (gongxuInfo.mokuaibiaoshi != 0) {
          // 生成订单
          const order_id = await this.model('cmswing/order').gongtoorder(id, userid);
          if (!order_id) { return this.fail('订单生成失败') }
        }
        const data = await this.model('gongxufabu').where({id: id}).find();
        return this.success(data, '确认成功');
      } else if (queren == 1) {
        const res = await this.model('gongxufabu').where({id: id}).update({status: 2});
        if (!res) { return this.fail('取消失败') }
        const data = await this.model('gongxufabu').where({id: id}).find();
        return this.success(data, '取消成功');
      }
      return this.fail('参数传递错误');
    }
    return this.fail('参数传递错误');
  }
  // 接受按钮
  async jieshouAction() {
    if (this.is_login > 0) {
      const id = this.post('id');
      const userId = this.is_login;
      const gongxuInfo = await this.model('gongxufabu').where({id: id, status: 3}).find();
      if (think.isEmpty(gongxuInfo)) {
        think.logger.info('jieshouAction:供需信息为空:id', id);
        return this.fail('接受失败');
      }
      const res = await this.model('gongxufabu').where({id: id}).update({status: 4});
      if (!res) { return this.fail('接受失败') }
      if (gongxuInfo.mokuaibiaoshi != 0) {
        // 生成订单
        const orderId = await this.model('cmswing/order').gongtoorder(id, userId);
        if (!orderId) { return this.fail('订单生成失败') }
      }
      return this.success({name: '接受成功', url: '/' + this.ctx.controller}, '接受成功');
    } else {
      return this.fail('接受失败');
    }
  }
  // 拒绝按钮
  async jujueAction() {
    if (this.is_login > 0) {
      const id = this.post('id');
      const gongxuInfo = await this.model('gongxufabu').where({id: id, status: 3}).find();
      if (think.isEmpty(gongxuInfo)) {
        think.logger.info('jujueAction:供需信息为空:id', id);
        return this.fail('拒绝失败');
      }
      const remarkData = this.post();
      remarkData.status = 2;
      remarkData.operation = '拒绝接单';
      remarkData.remark_type = this.ctx.controller;
      const {
        remarksId,
        updateData
      } = await this.addremark();
      updateData.recipient_remarks_id = remarksId;
      const res = await this.model('gongxufabu').where({id: id}).update(updateData);
      if (!res) { return this.fail('拒绝失败') }
      return this.success({name: '拒绝成功', url: '/' + this.ctx.controller}, '拒绝成功');
    } else {
      return this.fail('拒绝失败');
    }
  }
};
